import { defHttp } from '/@/utils/http/axios'

import {
  ProductVO,
  ProductBO,
  ProductSo,
  ProductLogSO,
  ProductLogVO,
  ProductPriceVO,
} from './model/productModel'
import { BasicFetchResult, BasicPageParams } from '/@/api/model/baseModel'

enum Api {
  pageProduct = '/product/page',
  createProduct = '/product/create',
  updateProduct = '/product/update',
  deleteProduct = '/product/delete',
  pageProductLog = '/product/log',
  exportStore = '/product/export',
  watchProduct = '/product/watch',
  unwatchProduct = '/product/unwatch',
  pageProductPrice = '/product/price/page',
  currentProduct = '/product/current',
}

export const pageProduct = (data: BasicPageParams<ProductSo>) => {
  return defHttp.post<BasicFetchResult<ProductVO>>(
    { url: Api.pageProduct, data },
    { errorMessageMode: 'message' },
  )
}

export const pageProductPrice = (data: BasicPageParams<ProductSo>) => {
  return defHttp.post<BasicFetchResult<ProductPriceVO>>(
    { url: Api.pageProductPrice, data },
    { errorMessageMode: 'message' },
  )
}

export const createProduct = (data: ProductBO) => {
  return defHttp.post<Boolean>({ url: Api.createProduct, data }, { errorMessageMode: 'message' })
}

export const updateProduct = (data: ProductBO) => {
  return defHttp.post<Boolean>({ url: Api.updateProduct, data }, { errorMessageMode: 'none' })
}

export const deleteProduct = (data: string) => {
  return defHttp.get<Boolean>(
    { url: Api.deleteProduct, params: { productId: data } },
    { errorMessageMode: 'message' },
  )
}

export const pageProductLogApi = (data: BasicPageParams<ProductLogSO>) => {
  return defHttp.post<BasicFetchResult<ProductLogVO>>(
    { url: Api.pageProductLog, data },
    { errorMessageMode: 'message' },
  )
}

export const exportStoreApi = (data: ProductSo) => {
  return defHttp.post<BlobPart>(
    {
      url: Api.exportStore,
      data,
      responseType: 'blob',
    },
    { errorMessageMode: 'message' },
  )
}

export const watchProduct = (data: string) => {
  return defHttp.get<Boolean>(
    { url: Api.watchProduct, params: { productId: data } },
    { errorMessageMode: 'message' },
  )
}

export const unwatchProduct = (data: string | undefined) => {
  return defHttp.get<Boolean>(
    { url: Api.unwatchProduct, params: { productId: data } },
    { errorMessageMode: 'message' },
  )
}

export const currentProduct = (data: string | undefined) => {
  return defHttp.get<ProductPriceVO>(
    { url: Api.currentProduct, params: { productId: data } },
    { errorMessageMode: 'message' },
  )
}
